﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WMemoryProfiler.Memory
{
    /// <summary>
    /// Contains a full sweep across all managed heaps. This type is used by MemoryDumper to self dump
    /// your manage heap stats. To get heap statistics of other processes use MdbEng.GetMemoryState()
    /// </summary>
    public class MemoryStatistics
    {
        /// <summary>
        /// Process name of debugged processs
        /// </summary>
        public string ProcessName
        {
            get;
            internal set;
        }

        /// <summary>
        /// Command lien args 
        /// </summary>
        public string CmdLineArgs
        {
            get;
            internal set;
        }

        /// <summary>
        /// Time when the memory statistics was taken.
        /// </summary>
        public DateTimeOffset DumpTaken
        {
            get;
            internal set;
        }

        /// <summary>
        /// Process running time
        /// </summary>
        public TimeSpan RunningTime
        {
            get;
            internal set;
        }

        /// <summary>
        /// This is the most interesting field which does contain as key the type name and as value the instance
        /// count and size.
        /// </summary>
        public Dictionary<string, InstanceInfo> ManagedHeapStats
        {
            get;
            private set;
        }

        /// <summary>
        /// Get from a process the number of allocated managed objects
        /// </summary>
        public long TotalInstanceCount
        {
            get
            {
                return ManagedHeapStats.Aggregate(0L, (acc, inst) => acc + inst.Value.InstanceCount);
            }
        }

        /// <summary>
        /// Get from a process the total number of bytes allocated on the managed heap
        /// </summary>
        public long TotalAllocatedMangedHeapBytes
        {
            get
            {
                return ManagedHeapStats.Aggregate(0L, (acc, inst) => acc + inst.Value.AllocatedBytes);
            }
        }

        /// <summary>
        /// Private bytes of process at the point of time the memory statistics was generated
        /// </summary>
        public long PrivateBytesInBytes
        {
            get;
            private set;
        }

        /// <summary>
        /// Working set in bytes of process at the point of time the memory statistics was generated
        /// </summary>
        public long WorkingSetInBytes
        {
            get;
            private set;
        }

        /// <summary>
        /// Virtual size in bytes of process at the point of time the memory statistics was generated
        /// </summary>
        public long VirtualSizeInBytes
        {
            get;
            private set;
        }

        /// <summary>
        /// Ctor to create a memory statistics instance
        /// </summary>
        /// <param name="memStat">Instance data created by MDbg.GetMemoryState()</param>
        /// <param name="privateBytes">Private bytes to debugged process</param>
        /// <param name="workingSet">Working set of debugged process.</param>
        /// <param name="virtualSize">Virtual Memory sized of debugged process.</param>
        public MemoryStatistics(Dictionary<string, InstanceInfo> memStat, long privateBytes, long workingSet, long virtualSize)
        {
            ManagedHeapStats = memStat;
            PrivateBytesInBytes = privateBytes;
            WorkingSetInBytes = workingSet;
            VirtualSizeInBytes = virtualSize;
        }
    }
}
